package com.example.studyapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import com.android.volley.VolleyError
import com.example.studyapp.data.jsonInteraction
import com.google.gson.Gson
import org.json.JSONObject
import org.w3c.dom.Text
import java.lang.Exception

//填空题布局
class study_complet_question_1 : AppCompatActivity() {
    lateinit var complet_question_text: TextView
    lateinit var complet_question_edit: EditText
    lateinit var complet_question_show: TextView
    lateinit var complet_question_prev: Button
    lateinit var complet_question_center: Button
    lateinit var complet_question_next: Button
    lateinit var complet_question_ret: Button
    lateinit var complet_question_title: TextView
    lateinit var complet_question_info: Button
    lateinit var jspis:ArrayList<jstruct_paper_item>
    var should_enabled_prev:Boolean=true
    var should_enabled_next:Boolean=true
    var i:Int=0
    var qui:Int=0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        }
        setContentView(R.layout.activity_study_complet_question_1)
        complet_question_text=this.findViewById(R.id.complet_question_text)
        complet_question_edit=this.findViewById(R.id.complet_question_edit)
        complet_question_show=this.findViewById(R.id.complet_question_show)
        complet_question_prev=this.findViewById(R.id.complet_question_prev)
        complet_question_center=this.findViewById(R.id.complet_question_center)
        complet_question_next=this.findViewById(R.id.complet_question_next)
        complet_question_ret=this.findViewById(R.id.complet_question_ret)
        complet_question_title=this.findViewById(R.id.complet_question_title)
        complet_question_info=this.findViewById(R.id.complet_question_info)
        qui= define_now_paper_info.choice_qu_num
        complet_question_title.text=define_now_paper_info.papername+"/填空"
        var jtmp:ArrayList<jstruct_paper_item> = arrayListOf<jstruct_paper_item>()
        //根据define_now_paper_info的内容，从服务器读取
        val jobj= JSONObject(Gson().toJson(jstruct_paper_item_send(item=qui,username= define_user_name,paper = define_study_qu_info_title)))
        jspis=jtmp
        object : jsonInteraction(jobj){
            override fun myResposeListener(response: JSONObject) {
                //处理获取的消息
                try{
                    val jipi:jstruct_paper_item=
                        Gson().fromJson(response.toString(),jstruct_paper_item::class.java)
                    set_view(jipi)
                    jtmp.add(jipi)
                    set_listener(jipi)
                }catch (e: Exception){
                    Toast.makeText(this@study_complet_question_1,"未能处理数据", Toast.LENGTH_LONG)
                        .show()
                }
            }
        }
    }

    fun set_view(jipi:jstruct_paper_item) {
        if(should_enabled_prev){
            complet_question_prev.isEnabled=false
            complet_question_prev.isVisible=false
        }
        if(should_enabled_next){
            complet_question_next.isEnabled=false
            complet_question_next.isVisible=false
        }
        complet_question_text.text=jipi.text
        complet_question_info.text=(i+1).toString()+"/"+ define_now_paper_info.completion_qu_num.toString()
    }
    fun set_listener(jipi:jstruct_paper_item) {
        complet_question_center.setOnClickListener {
            //看答案
            complet_question_show.text= jipi.answer
            if(should_enabled_prev){
                complet_question_prev.isEnabled=true
                complet_question_prev.isVisible=true
            }
            if(should_enabled_next){
                complet_question_next.isEnabled=true
                complet_question_next.isVisible=true
            }

        }
        complet_question_ret.setOnClickListener {
            this.finish()
        }
        complet_question_prev.setOnClickListener {
            //切换上一道题
            if(i<=0){//禁止切换
                return@setOnClickListener
            }
            should_enabled_next=false
            should_enabled_prev=false
            try{
                i-=1
                set_view(jspis[i])
            }catch (e:Exception){
                //Toast.makeText(this@study_choice_question_1,"未能处理数据",Toast.LENGTH_LONG)
            }
        }
        complet_question_next.setOnClickListener {
            //切换下一道题
            i+=1
            if(jspis.size>i){//说明题目已经存入jspis
                set_view(jspis[i])
            }
            else{//获取新题
                should_enabled_next=true
                should_enabled_prev=true
                //判断这是不是最后一题
                if(i>= define_now_paper_info.completion_qu_num){
                    complet_question_next.isEnabled=false
                    complet_question_next.isVisible=false
                    should_enabled_next=false
                }
                val jobj=JSONObject(Gson().toJson(jstruct_paper_item_send(item=i+qui,username= define_user_name,paper = define_study_qu_info_title)))
                object :jsonInteraction(jobj){
                    override fun myResposeListener(response: JSONObject) {
                        //处理获取的消息
                        try{
                            val jtmp:jstruct_paper_item=Gson().fromJson(response.toString(),jstruct_paper_item::class.java)
                            set_view(jtmp)
                            jspis.add(jtmp)
                        }catch (e:Exception){
                            //Toast.makeText(this@study_choice_question_1,"未能处理数据",Toast.LENGTH_LONG)
                        }
                    }
                }

            }
        }

    }

}